package com.syn.michat.service.impl;

import com.syn.michat.constant.ErrorContants;
import com.syn.michat.constant.FriendContants;
import com.syn.michat.mapper.FriendsMapper;
import com.syn.michat.pojo.Friend;
import com.syn.michat.service.FriendsService;
import com.syn.michat.util.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 好友服务
 **/

@Service
public class FriendsServiceImpl implements FriendsService {

    @Autowired
    private FriendsMapper friendsMapper;

    @Override
    public int request(Friend friend) throws ClientException {
        try {
            Friend friend1 = friendsMapper.findFriend(friend.getFriendId(), friend.getUserId());
            Friend friend2 = friendsMapper.findFriend(friend.getUserId(), friend.getFriendId());

            if (friend1 != null && friend1.getStatus() == FriendContants.FRIEND
                    && friend2 != null && friend2.getStatus() == FriendContants.FRIEND) {
                throw new ClientException(ErrorContants.Friend_Already, "已是好友");
            }

            if (friend2 == null) {
                friendsMapper.request(friend);
                return FriendContants.APPROVAL_REQUIRED;
            } else {
                // 对方和自己还是好友则直接修改状态，无需同意
                if  (friend2.getStatus() == FriendContants.FRIEND) {
                    friendsMapper.updateFriendStatus(friend.getFriendId(), friend.getUserId(), FriendContants.FRIEND);
                    return FriendContants.ADDED_SUCCESSFULLY;
                } else {
                    friendsMapper.request(friend);
                    return FriendContants.APPROVAL_REQUIRED;
                }
            }
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public List<Friend> queryAllApplyRecords(String userId) throws ClientException {
        try {
            return friendsMapper.quaryAllFriendRequest(userId, FriendContants.REQUEST);
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public void approvalFriend(String userId, String friendId, int isAgree) throws ClientException {
        try {
            if (isAgree == 0) {
                friendsMapper.updateFriendStatus(userId, friendId, FriendContants.DISAGREE);
            } else if (isAgree == 1) {
                friendsMapper.updateFriendStatus(userId, friendId, FriendContants.FRIEND);
                Friend friend = new Friend();
                friend.setFriendId(userId);
                friend.setUserId(friendId);
                friend.setStatus(FriendContants.FRIEND);
                friendsMapper.request(friend);
            }
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public List<Friend> queryAllFriends(String userId) throws ClientException {
        try {
            return friendsMapper.quaryAllFriends(userId, FriendContants.FRIEND);
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public Friend findFriend(String userId, String friendId) throws ClientException {
        try {
            return friendsMapper.findFriend(userId, friendId);
        } catch (NullPointerException e) {
            return null;
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }

    @Override
    public void deleteFriend(String userId, String friendId) throws ClientException {
        try {
            friendsMapper.updateFriendStatus(userId, friendId, FriendContants.DELETE);
        } catch (Exception e) {
            throw new ClientException(ErrorContants.SQL_INCORRECT, e.getMessage());
        }
    }
}
